﻿using System.Collections.Generic;

namespace Core.STL
{
    /// <summary>
    /// 封装C#原生List，用于提升List的删除效率
    /// 逻辑：交换需要删除的元素与最后一个元素的，Count--；
    /// 注意：删除元素会打乱元素之间的顺序，无法用于需要保证顺序的使用场景
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class MList<T>
    {
        private readonly List<T> list = new();

        public int Count { get; private set; }

        public void Add(T t)
        {
            if (Count >= list.Count)
            {
                list.Add(t);
            }
            list[Count] = t;
        }

        //public System.Collections.Generic.IEnumerable<T> Foreach

        public void RemoveAt(int index)
        {
            if (index < 0 || index >= Count)
            {
                return;
            }
            if (index == Count - 1)
            {
                list[Count - 1] = default;
                Count--;
            }
            else
            {
                list[index] = list[Count - 1];
                list[Count - 1] = default;
                Count--;
            }
        }
    }
}
